<?php
abstract class Pageley_Controller_Command_Abstract implements Pageley_Controller_Command {

    public function execute() {
        $modelAndView = null;
        try {
            $modelAndView = $this->executeInternal();
        } catch(Pageley_Domain_Exception $e) {
            throw new Pageley_Controller_Exception("Command execution failed.", 0, $e, 
                $this->getFailureAction());
        }
        if($modelAndView === null) {
            $modelAndView = new Pageley_Controller_ModelAndView();
        }
        $modelAndView->setAfterViewAction($this->getSuccessAction());
        return $modelAndView;
    }

	protected abstract function executeInternal();

	public function __toString() {
		return get_class($this);
	}

    protected function getSession() {
        $factory = Pageley_Controller_Session_Factory::getInstance();
        return $factory->getSession();
    }
    
    protected function getFailureAction() {
        return null;
    }
    
    protected function getSuccessAction() {
        return null;
    }

    protected function getClientAddress() {
        $address = "127.0.0.1";
		if(array_key_exists("remote_addr", $_SERVER)) {
			$address = $_SERVER["remote_addr"];
		}
        return $address;
    }
}